clear;
%%%% Macro data obtained from FRED
% PCE: total
fred_Cons = getFredData('PCE', '1971-01-01', '2018-12-31','lin','a','avg');
Cons = fred_Cons.Data(:, 2);

% Private investment
fred_Inv = getFredData('GPDI', '1971-01-01', '2018-12-31','lin','a','avg');
fred_Inv_housing = getFredData('PRFI', '1971-01-01', '2018-12-31','lin','a','avg');
fred_Inv_change_of_inventory = getFredData('CBI', '1971-01-01', '2018-12-31','lin','a','avg');
    
Inv = fred_Inv.Data(:, 2) - 0 * fred_Inv_housing.Data(:, 2) - fred_Inv_change_of_inventory.Data(:, 2);

% Government investment
fred_G_inv = getFredData('A782RC1Q027SBEA', '1971-01-01', '2018-12-31','lin','a','avg');
G_inv = fred_G_inv.Data(:, 2);

% Government investment: national defense
fred_G_inv_def = getFredData('DGI', '1971-01-01', '2018-12-31','lin','a','avg');
G_inv_def = fred_G_inv_def.Data(:, 2);

% G: consumption expenditures
fred_G = getFredData('A955RC1Q027SBEA', '1971-01-01', '2018-12-31','lin','a','avg');
G_exp = fred_G.Data(:, 2);

% money_holding (corporate and non-corporate) in millions
fred_m_noncorp = getFredData('NNBCDEA027N', '1971-01-01', '2018-12-31','lin','a','avg');
fred_m_corp    = getFredData('NCBDCAA027N', '1971-01-01', '2018-12-31','lin','a','avg');
money = (fred_m_noncorp.Data(:, 2) + fred_m_corp.Data(:, 2)) / 1000;

% nominal GDP 
fred_gdp = getFredData('GDP', '1971-01-01', '2018-12-31','lin','a','avg');
gdp = fred_gdp.Data(:, 2);

% Deflator: national_defense
fred_G_inv_def_p = getFredData('A824RD3Q086SBEA', '1971-01-01', '2018-12-31','lin','a','avg');
G_inv_def_p = fred_G_inv_def_p.Data(:, 2);

% Deflator: private_investment
fred_inv_p = getFredData('A006RD3Q086SBEA', '1971-01-01', '2018-12-31','lin','a','avg');
deflator_inv = fred_inv_p.Data(:, 2);

% Deflator: private residential investment
fred_inv_housing_p = getFredData('A011RD3Q086SBEA', '1971-01-01', '2018-12-31','lin','a','avg');
deflator_inv_housing = fred_inv_housing_p.Data(:, 2);

% Deflator: business
fred_business_p = getFredData('B358RG3Q086SBEA', '1971-01-01', '2018-12-31','lin','a','avg');
deflator_bv = fred_business_p.Data(:, 2);

% Inflaton
fred_CPI = getFredData('CPIAUCSL', '1970-12-31', '2018-12-31','lin','a','eop');
CPI = fred_CPI.Data(:, 2);
%PPI_inflation = PPI_inflation / 100 + 1;
inflation = CPI(2:end) ./ CPI(1:end-1);
save inflation_by_CPI inflation

% interest rate
fred_T_bill_3m = getFredData('TB3MS', '1971-01-01', '2018-12-31','lin','a','avg');
T_bill_3m = fred_T_bill_3m.Data(:, 2);

fred_T_bill_1y = getFredData('DGS1', '1971-01-01', '2018-12-31','lin','a','avg');
T_bill_1y = fred_T_bill_1y.Data(:, 2);

fred_T_bill_10y = getFredData('GS10', '1971-01-01', '2018-12-31','lin','a','avg');
T_bill_10y = fred_T_bill_10y.Data(:, 2);

fred_AAA = getFredData('AAA', '1971-01-01', '2018-12-31','lin','a','avg');
AAA_rate = fred_AAA.Data(:, 2);

fred_BAA = getFredData('BAA', '1971-01-01', '2018-12-31','lin','a','avg');
BAA_rate = fred_BAA.Data(:, 2);

fred_mortgage_rate = getFredData('MORTGAGE30US', '1971-01-01', '2018-12-31','lin','a','avg');
mortgage_rate = fred_mortgage_rate.Data(:, 2);


% real private consumption
fred_C_real = getFredData('PCECC96', '1971-01-01', '2018-12-31','lin','a','avg');
C_real = fred_C_real.Data(:, 2);

% real private investment
%%%% with housing
fred_I_real = getFredData('GPDIC1', '1971-01-01', '2018-12-31','lin','a','avg');
fred_I_real_inventory = getFredData('CBIC1', '1971-01-01', '2018-12-31','lin','a','avg');
%%%% without housing
I_real = fred_I_real.Data(:, 2) - fred_I_real_inventory.Data(:,2)...
    - 0 * fred_Inv_housing.Data(:, 2) ./ fred_inv_housing_p.Data(:,2) * 100;

% real government expenditures + investment
fred_G_real_E_plus_I = getFredData('GCEC1', '1971-01-01', '2018-12-31','lin','a','avg');
G_real_E_plus_I = fred_G_real_E_plus_I.Data(:, 2);

% real government expenditures
fred_G_real_E = getFredData('A955RX1Q020SBEA', '1971-01-01', '2018-12-31','lin','a','avg');
G_real_E = fred_G_real_E.Data(:, 2);

% Hours of work
fred_Hours = getFredData('B4701C0A222NBEA', '1971-01-01', '2018-12-31','lin','a','avg');
Hours = fred_Hours.Data(:, 2);
save('FredData')
